package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.InspectionDefectRule;
import com.ruibang.glass.quality.domain.req.InspectionDefectStatisticsRuleReq;
import com.ruibang.glass.quality.entity.InspectionDefectStatisticsRule;
import com.ruibang.glass.quality.mapper.InspectionDefectStatisticsRuleMapper;
import com.ruibang.glass.quality.service.InspectionDefectStatisticsRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-02-23  14:33
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class InspectionDefectStatisticsRuleServiceImpl extends ServiceImpl<InspectionDefectStatisticsRuleMapper, InspectionDefectStatisticsRule> implements InspectionDefectStatisticsRuleService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(List<InspectionDefectStatisticsRuleReq> inspectionDefectStatisticsRule) {
        if (CollectionUtils.isNotEmpty(inspectionDefectStatisticsRule)) {
            //删除
            QueryWrapper<InspectionDefectStatisticsRule> qw = new QueryWrapper<>();
            qw.isNotNull("rule_id");
            this.remove(qw);
            //新增
            List<InspectionDefectStatisticsRule> ruleList = new ArrayList<>();
            for (InspectionDefectStatisticsRuleReq inspectionDefectStatisticsRuleReq : inspectionDefectStatisticsRule) {
                List<InspectionDefectRule> inspectionDefectRuleInfo = inspectionDefectStatisticsRuleReq.getInspectionDefectRule();
                if (CollectionUtils.isNotEmpty(inspectionDefectRuleInfo)) {
                    for (InspectionDefectRule inspectionDefectRule : inspectionDefectRuleInfo) {
                        if (inspectionDefectRule.getThresholdValueOne() != null && inspectionDefectRule.getThresholdValueTwo() != null && StringUtils.isNotBlank(inspectionDefectRule.getCoordinate())
                                && StringUtils.isNotBlank(inspectionDefectRule.getComparisonOperatorOne()) && StringUtils.isNotBlank(inspectionDefectRule.getComparisonOperatorTwo())) {
                            InspectionDefectStatisticsRule rule = new InspectionDefectStatisticsRule();
                            rule.setRuleName(inspectionDefectRule.getRuleName());
                            rule.setComparisonOperatorOne(inspectionDefectRule.getComparisonOperatorOne());
                            rule.setThresholdValueOne(inspectionDefectRule.getThresholdValueOne());
                            rule.setComparisonOperatorTwo(inspectionDefectRule.getComparisonOperatorTwo());
                            rule.setThresholdValueTwo(inspectionDefectRule.getThresholdValueTwo());
                            rule.setCoordinate(inspectionDefectRule.getCoordinate());
                            rule.setRuleType(inspectionDefectStatisticsRuleReq.getRuleType());
                            ruleList.add(rule);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(ruleList)) {
                this.saveBatch(ruleList);
            }
        }
    }

    @Override
    public List<InspectionDefectStatisticsRuleReq> getRule() {
        List<InspectionDefectStatisticsRule> rule = this.list();
        if (CollectionUtils.isNotEmpty(rule)) {
            List<InspectionDefectStatisticsRuleReq> inspectionDefectStatisticsRuleReqs = new ArrayList<>();
            Map<String, List<InspectionDefectStatisticsRule>> ruleMap = rule.stream().collect(Collectors.groupingBy(InspectionDefectStatisticsRule::getRuleType));
            ruleMap.forEach((k, v) -> {
                InspectionDefectStatisticsRuleReq inspectionDefectStatisticsRuleReq = new InspectionDefectStatisticsRuleReq();
                inspectionDefectStatisticsRuleReq.setRuleType(k);
                List<InspectionDefectRule> inspectionDefectRuleInfo = new ArrayList<>();
                for (InspectionDefectStatisticsRule inspectionDefectRule : v) {
                    InspectionDefectRule ruleInfo = new InspectionDefectRule();
                    ruleInfo.setComparisonOperatorOne(inspectionDefectRule.getComparisonOperatorOne());
                    ruleInfo.setThresholdValueOne(inspectionDefectRule.getThresholdValueOne());
                    ruleInfo.setComparisonOperatorTwo(inspectionDefectRule.getComparisonOperatorTwo());
                    ruleInfo.setThresholdValueTwo(inspectionDefectRule.getThresholdValueTwo());
                    ruleInfo.setCoordinate(inspectionDefectRule.getCoordinate());
                    ruleInfo.setRuleName(inspectionDefectRule.getRuleName());
                    inspectionDefectRuleInfo.add(ruleInfo);
                }
                inspectionDefectStatisticsRuleReq.setInspectionDefectRule(inspectionDefectRuleInfo);
                inspectionDefectStatisticsRuleReqs.add(inspectionDefectStatisticsRuleReq);
            });
            return inspectionDefectStatisticsRuleReqs;
        }
        return Collections.emptyList();
    }
}
